package com.iSnail.ms.gather.ui;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.iSnail.ms.util.Config;
import com.iSnail.ms.util.Const;
import com.iSnail.ms.util.I18N;

public class StartStopPanel extends JPanel implements ActionListener{
	JButton start = null;
	JButton stop = null;
	int frequency = 1;
	JComboBox frequencybox = null;
	boolean gatherstatus = false;
	String title = null;
	
	public StartStopPanel(String title){
		this.title = title;
		setLayout(new FlowLayout());
		start = new JButton(I18N.getString("start_treenode"), new ImageIcon("resource/pic/start.gif"));
		start.setActionCommand("start");
		start.addActionListener(this);

		stop = new JButton(I18N.getString("stop_treenode"), new ImageIcon("resource/pic/stop.gif"));
		stop.setActionCommand("stop");
		stop.addActionListener(this);
		stop.setEnabled(false);

		add(getFrequencyBoxPanel());
		add(start);
		add(stop);
	}
	
	private JPanel getFrequencyBoxPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(1, 2));
		String[] times = Config.getConfigValues(Const.PORT_GATHER_FREQUENCY);
		frequencybox = new JComboBox(times);
		frequencybox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setFrequency(frequencybox.getSelectedIndex() + 1);
			}
		});
		JLabel frequencyname = new JLabel(I18N.getString("time_frequency"));
		panel.add(frequencyname);
		panel.add(frequencybox);
		return panel;
	}
	
	private final void setFrequency(int frequency) {
		this.frequency = frequency;
	}
	
	public int getGatherFrequency(){
		return this.frequency;
	}
	
	public boolean getGatherStatus(){
		return this.gatherstatus;
	}
	
	public void actionPerformed(ActionEvent event) {
		if (event.getActionCommand().equals("start")) {
			gatherstatus = true;
			start.setEnabled(false);
			stop.setEnabled(true);
			MainFrame.setConsoleInfo(I18N.getString("start_treenode")+title);
		} else if (event.getActionCommand().equals("stop")) {
			gatherstatus = false;
			stop.setEnabled(false);
			start.setEnabled(true);
			MainFrame.setConsoleInfo(I18N.getString("stop_treenode")+title);
		}
	}
}